package Manager

import (
	"deepgox-go-admin/Infrastructures/Rom"
	"deepgox-go-admin/Models/Manager"
)

// LoginModelHandler 用户注册模型
type LoginModelHandler struct {
	Manager.LoginModelHandler
}

// LoginManager 用户登陆服务
// 用户登陆后有用户权限，用户服务规则，用户数据三组数据
func (managerModel *LoginModelHandler) LoginManager() (Manager.Manager, error) {
	var manager Manager.Manager
	Rom.Db.Raw("SELECT *"+
		"FROM manager  where manager.username = ? and manager.password = ?", managerModel.UserName, managerModel.Password).Scan(&manager)
	return manager, nil
}

// ByIdModelHandler 用户ID查询模型
type ByIdModelHandler struct {
	Manager.ByIdModelHandler
}

// ByIdManager 根据ID查找用户信息
func (managerModel *ByIdModelHandler) ByIdManager() (Manager.Manager, error) {
	var manager Manager.Manager
	Rom.Db.Raw("SELECT *"+
		"FROM manager  where manager.id = ? ", managerModel.Id).Scan(&manager)
	return manager, nil
}

// RegModelHandler 管理员注册服务模型
type RegModelHandler struct {
	Manager.AddModelHandler
}

func (managerModel *RegModelHandler) AddManager() (int64, error) {
	result := Rom.Db.Exec("INSERT into manager(username,`password`,email,gender) VALUES(?, ?, ? , ? )", managerModel.UserName, managerModel.Password, managerModel.Email, managerModel.Gender)
	if result.RowsAffected != 1 {
		return 0, result.Error
	}
	return result.RowsAffected, nil
}
